package com.example.agriculturemachine.ui.activity;

import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.agriculturemachine.R;
import com.example.agriculturemachine.base.BaseActivity;
import com.example.agriculturemachine.base.BaseFragment;
import com.example.agriculturemachine.ui.fragment.bindingFragment;
import com.example.agriculturemachine.ui.fragment.managerFragment;
import com.example.agriculturemachine.ui.fragment.personalFragment;
import com.example.agriculturemachine.ui.fragment.selectFragment;
import com.example.agriculturemachine.untils.LogUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;

public class MainActivity extends BaseActivity {


    private bindingFragment mBindingFragment;
    private selectFragment mSelectFragment;
    private FragmentManager mFm;
    private FragmentTransaction mTransaction;

    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;
    private personalFragment mPersonalFragment;

    @BindView(R.id.main_pager_container)
    public FrameLayout main_pager_container;
    private managerFragment mManagerFragment;

    public Boolean flag;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEvent() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                if(item.getItemId()==R.id.car_manager){
                    LogUtils.d(MainActivity.this,"切换到 "+item.getTitle());

//                        LogUtils.d(this,"执行到了-->");
                        switchFragment(mBindingFragment);

                }else if(item.getItemId()==R.id.personal){
                    LogUtils.d(MainActivity.this,"切换到 "+item.getTitle());
                    switchFragment(mPersonalFragment);
                }


                return true;
            }
        });

    }
//    public void setFlag(Boolean flagTest){
//        flag=flagTest;
//        LogUtils.d(this,"flag -->"+flag);
//    }

    @Override
    protected void initView() {
        initFragment();
    }


    private void initFragment() {
        mBindingFragment = new bindingFragment();
        mManagerFragment = new managerFragment();
        mSelectFragment= new selectFragment();
        mPersonalFragment = new personalFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mBindingFragment);
    }

    private BaseFragment lastOneFragment=null;

    public void switchFragment(BaseFragment fragment) {
        mTransaction = mFm.beginTransaction();
        if(!fragment.isAdded()){
            mTransaction.add(R.id.main_pager_container,fragment);
        }else{
            mTransaction.show(fragment);
        }
        if(lastOneFragment!=null&&lastOneFragment!=fragment){
            mTransaction.hide(lastOneFragment);
        }
        lastOneFragment=fragment;
        mTransaction.commit();

    }


}